<?php
/**
 * Created by PhpStorm.
 * User: nightelf
 * Date: 2020/1/19
 * Time: 16:17
 */

namespace App\Http\Controllers\Warehouse;


use App\Exceptions\ApiException;
use App\Http\Controllers\Controller;
use App\Http\Model\SettingModel;
use App\Http\Model\SupplierOrdersModel;
use App\Http\Model\SupplierOrdersReturnedModel;
use App\Http\Model\WarehouseAdjustInfoModel;
use App\Http\Model\WarehouseAdjustModel;
use App\Http\Model\WarehouseLogInfoModel;
use App\Http\Model\WarehouseLogModel;
use App\Http\Model\WarehouseModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class WarehouseController extends Controller
{
    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function lists(Request $request)
    {
        $rules = [
            'projectId' => 'required|integer',
            'buyType' => 'nullable|integer|in:1,2',
            'type' => 'nullable|integer',
            'length' => 'nullable|integer|in:10,20,50',
            'page' => 'nullable|integer|min:0',
        ];
        $message = [
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型不正确',
            'buyType.integer' => '采购类型参数类型错误',
            'buyType.min' => '采购类型参数值不小于:min',
            'type.integer' => '材料类型参数类型错误',
            'length.integer' => '每页记录条数参数类型错误',
            'length.in' => '每页记录参数值不正确',
            'page.integer' => '起始记录参数类型错误',
            'page.min' => '起始记录参数值不小于:min',
        ];
        $param = $request->only(['projectId', 'search', 'length', 'type', 'page']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $warehouseModel = new WarehouseModel();
            $lists = $warehouseModel->lists($param);
            $countLists = $warehouseModel->countLists($param);
            $this->data = $this->tableFormat($lists, $countLists);
        } else {
            throw new ApiException($validator->errors()->first(), 610101);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function info(Request $request)
    {
        $rules = [
            'id' => 'required|integer',
        ];
        $message = [
            'id.required' => '获取仓库材料参数失败',
            'id.integer' => '仓库材料参数类型不正确',
        ];
        $param = $request->only(['id']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $warehouseModel = new WarehouseModel();
            $this->data = $warehouseModel->info($param);
        } else {
            throw new ApiException($validator->errors()->first(), 610201);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function setSalePrice(Request $request)
    {
        $rules = [
            'id' => 'required|integer',
            'salePrice' => 'required|numeric',
        ];
        $message = [
            'id.required' => '获取仓库材料参数失败',
            'id.integer' => '仓库材料参数类型不正确',
            'salePrice.required' => '请填写售价',
            'salePrice.numeric' => '售价参数类型不正确',
        ];
        $param = $request->only(['id', 'salePrice']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $warehouseModel = new WarehouseModel();
            $id = $param['id'];
            unset($param['id']);
            $info = $warehouseModel->info(['id' => $id]);
            if (!empty($info)) {
                $settingModel = new SettingModel();
                $saleRate = $settingModel->get('saleRate', $info['projectId']);
                $purchasePrice = $info['purchasePrice'];
                $result = $param['salePrice'] - $purchasePrice * (1 + $saleRate);
                if ($result < 0) {
                    throw new ApiException('售价不能高于材料底价', 610302);
                }
                $warehouseModel->update(['id' => $id], $param);
            }
        } else {
            throw new ApiException($validator->errors()->first(), 610301);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function inLog(Request $request)
    {
        $rules = [
            'projectId' => 'required|integer',
            'materialId' => 'nullable|integer',
            'specId' => 'nullable|integer',
            'sourceEmployeeId' => 'nullable|integer',
            'sourceGroupId' => 'nullable|integer',
            'sourceProjectId' => 'nullable|integer',
            'month' => 'nullable|date_format:Y-m',
            'startTime' => 'nullable|date_format:Y-m-d',
            'endTime' => 'nullable|date_format:Y-m-d',
            'type' => 'nullable|integer|in:1,2,3,4,5,6,7',
            'status' => 'nullable|integer|in:0,1',
            'length' => 'nullable|integer|in:10,20,50',
            'page' => 'nullable|integer|min:0',
        ];
        $message = [
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型不正确',
            'materialId.integer' => '项目参数类型不正确',
            'specId.integer' => '项目参数类型不正确',
            'sourceEmployeeId.integer' => '出库目标工人参数类型不正确',
            'sourceGroupId.integer' => '出库目标班组参数类型不正确',
            'sourceProjectId.integer' => '出库目标项目参数类型不正确',
            'startTime.date_format' => '起始时间格式不正确',
            'endTime.date_format' => '起始时间格式不正确',
            'type.integer' => '记录条数参数类型错误',
            'type.in' => '记录条数参数值不正确',
            'status.integer' => '记录条数参数类型错误',
            'status.in' => '记录条数参数值不正确',
            'length.integer' => '记录条数参数类型错误',
            'length.in' => '记录条数参数值不正确',
            'page.integer' => '页码参数类型错误',
            'page.min' => '页码参数值不小于:min',
        ];
        $param = $request->all();
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $warehouseLogInfoModel = new WarehouseLogInfoModel();
            if (empty($param['type'])){
                $param['type'] = [1,2];
            }else{
                $param['type'] = [$param['type']];
            }
            $lists = $warehouseLogInfoModel->lists($param);
            $countLists = $warehouseLogInfoModel->countLists($param);
            $this->data = $this->tableFormat($lists, $countLists);
        } else {
            throw new ApiException($validator->errors()->first(), 610401);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function outLog(Request $request)
    {
        $rules = [
            'projectId' => 'required|integer',
            'materialId' => 'nullable|integer',
            'specId' => 'nullable|integer',
            'sourceEmployeeId' => 'nullable|integer',
            'sourceGroupId' => 'nullable|integer',
            'sourceProjectId' => 'nullable|integer',
            'month' => 'nullable|date_format:Y-m',
            'startTime' => 'nullable|date_format:Y-m-d',
            'endTime' => 'nullable|date_format:Y-m-d',
            'type' => 'nullable|integer|in:1,2,3,4,5,6,7',
            'status' => 'nullable|integer|in:0,1',
            'length' => 'nullable|integer|in:10,20,50',
            'page' => 'nullable|integer|min:0',
        ];
        $message = [
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型不正确',
            'materialId.integer' => '项目参数类型不正确',
            'specId.integer' => '项目参数类型不正确',
            'sourceEmployeeId.integer' => '出库目标工人参数类型不正确',
            'sourceGroupId.integer' => '出库目标班组参数类型不正确',
            'sourceProjectId.integer' => '出库目标项目参数类型不正确',
            'startTime.date_format' => '起始时间格式不正确',
            'endTime.date_format' => '起始时间格式不正确',
            'type.integer' => '记录条数参数类型错误',
            'type.in' => '记录条数参数值不正确',
            'status.integer' => '记录条数参数类型错误',
            'status.in' => '记录条数参数值不正确',
            'length.integer' => '记录条数参数类型错误',
            'length.in' => '记录条数参数值不正确',
            'page.integer' => '页码参数类型错误',
            'page.min' => '页码参数值不小于:min',
        ];
        $param = $request->all();
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $warehouseLogInfoModel = new WarehouseLogInfoModel();
            if (empty($param['type'])){
                $param['type'] = [3,4,5,6,7,8];
            }else{
                $param['type'] = [$param['type']];
            }
            $lists = $warehouseLogInfoModel->lists($param);
            $countLists = $warehouseLogInfoModel->countLists($param);
            $this->data = $this->tableFormat($lists, $countLists);
        } else {
            throw new ApiException($validator->errors()->first(), 610401);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function materialLog(Request $request){
        $rules = [
            'projectId' => 'required|integer',
            'materialId' => 'nullable|integer',
            'specId' => 'nullable|integer',
            'startTime' => 'nullable|date_format:Y-m-d',
            'endTime' => 'nullable|date_format:Y-m-d',
            'type' => 'nullable|integer|in:1,2,3,4,5,6,7,8',
            'status' => 'nullable|integer|in:0,1',
            'length' => 'nullable|integer|in:10,20,50',
            'page' => 'nullable|integer|min:0',
        ];
        $message = [
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型不正确',
            'materialId.integer' => '材料参数类型不正确',
            'specId.integer' => '规格参数类型不正确',
            'startTime.date_format' => '起始时间格式不正确',
            'endTime.date_format' => '起始时间格式不正确',
            'type.integer' => '记录条数参数类型错误',
            'type.in' => '记录条数参数值不正确',
            'status.integer' => '记录条数参数类型错误',
            'status.in' => '记录条数参数值不正确',
            'length.integer' => '记录条数参数类型错误',
            'length.in' => '记录条数参数值不正确',
            'page.integer' => '页码参数类型错误',
            'page.min' => '页码参数值不小于:min',
        ];
        $param = $request->only('projectId','materialId','startTime','endTime','type','specId');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $warehouseLogInfoModel = new WarehouseLogInfoModel();
            $lists = $warehouseLogInfoModel->lists($param);
            $countLists = $warehouseLogInfoModel->countLists($param);
            $this->data = $this->tableFormat($lists, $countLists);
        } else {
            throw new ApiException($validator->errors()->first(), 610401);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * 材料采购
     *
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function purchase(Request $request)
    {
        $rules = [
            'projectId' => 'required|integer',
            'supplierId' => 'required|integer',
            'name' => 'required_if:supplierId,0',
            'phone' => 'required_if:supplierId,0',
            'address' => 'required_if:supplierId,0',
            'deliveryTime' => 'required|date_format:Y-m-d',
            'price' => 'required|numeric',
            'payType' => 'required|integer|in:1,2,3',
            'data' => 'required|array',
        ];
        $message = [
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型不正确',
            'supplierId.required' => '获取供应商参数失败',
            'supplierId.integer' => '供应商参数类型不正确',
            'name.required_if' => '请填写供应商名称',
            'phone.required_if' => '请填写供应商电话',
            'address.required_if' => '请填写供应商地址',
            'price.required' => '获取总价失败',
            'price.numeric' => '总价参数类型不正确',
            'deliveryTime.required' => '请选择订单日期',
            'deliveryTime.date_format' => '订单日期格式不正确',
            'payType.required' => '请选择付款方式',
            'payType.integer' => '付款方式参数类型错误',
            'payType.in' => '付款方式参数不正确',
            'data.required' => '请填写材料具体信息',
            'data.array' => '材料具体信息类型不正确',
        ];
        $param = $request->only(['orderId','projectId', 'supplierId', 'name', 'phone', 'address', 'price', 'data','deliveryTime', 'payType', 'remark']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $rules1 = [
                'materialId' => 'required|integer',
                'specId' => 'required|integer',
                'unitId' => 'required|integer',
                'amount' => 'required|numeric',
                'price' => 'required|numeric',
                'totalPrice' => 'required|numeric',
            ];
            $message1 = [
                'materialId.required' => '请选择材料',
                'materialId.integer' => '材料参数类型错误',
                'specId.required' => '请选择材料规格',
                'specId.integer' => '材料规格参数类型错误',
                'unitId.required' => '请选择计量单位',
                'unitId.integer' => '计量单位参数类型错误',
                'amount.required' => '请填写材料数量',
                'amount.numeric' => '材料数量类型错误',
                'price.required' => '请填写材料价格',
                'price.numeric' => '材料价格类型错误',
                'totalPrice.required' => '未获取材料总价',
                'totalPrice.numeric' => '材料总价类型错误',
            ];
            $param1 = $param['data'];
            foreach ($param1 as $key => $i) {
                $validator1 = Validator::make($i, $rules1, $message1);
                if ($validator1->fails()) {
                    throw new ApiException($validator1->errors()->first(), 610502);
                }
            }

            $supplierOrdersModel = new SupplierOrdersModel();
            $param['adminId'] = $request->session()->get('adminId');
            $result = $supplierOrdersModel->addOrder($param);
            if (!$result){
                throw new ApiException('保存失败', 610503);
            }
        } else {
            throw new ApiException($validator->errors()->first(), 610501);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * 材料采购
     *
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function returnedPurchase(Request $request)
    {
        $rules = [
            'projectId' => 'required|integer',
            'supplierId' => 'required|integer',
            'returnedTime' => 'required|date_format:Y-m-d',
            'price' => 'required|numeric',
            'payType' => 'required|integer|in:0',
            'data' => 'required|array',
        ];
        $message = [
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型不正确',
            'supplierId.required' => '获取供应商参数失败',
            'supplierId.integer' => '供应商参数类型不正确',
            'price.required' => '获取总价失败',
            'price.numeric' => '总价参数类型不正确',
            'returnedTime.required' => '请选择退货日期',
            'returnedTime.date_format' => '退货日期格式不正确',
            'payType.required' => '请选择付款方式',
            'payType.integer' => '付款方式参数类型错误',
            'payType.in' => '付款方式参数不正确',
            'data.required' => '请填写材料具体信息',
            'data.array' => '材料具体信息类型不正确',
        ];
        $param = $request->only(['projectId', 'supplierId', 'price', 'data','returnedTime', 'payType', 'remark']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $rules1 = [
                'materialId' => 'required|integer',
                'specId' => 'required|integer',
                'unitId' => 'required|integer',
                'amount' => 'required|numeric',
                'price' => 'required|numeric',
                'totalPrice' => 'required|numeric',
            ];
            $message1 = [
                'materialId.required' => '请选择材料',
                'materialId.integer' => '材料参数类型错误',
                'specId.required' => '请选择材料规格',
                'specId.integer' => '材料规格参数类型错误',
                'unitId.required' => '请选择计量单位',
                'unitId.integer' => '计量单位参数类型错误',
                'amount.required' => '请填写材料数量',
                'amount.numeric' => '材料数量类型错误',
                'price.required' => '请填写材料价格',
                'price.numeric' => '材料价格类型错误',
                'totalPrice.required' => '未获取材料总价',
                'totalPrice.numeric' => '材料总价类型错误',
            ];
            $param1 = $param['data'];
            $warehouseModel = new WarehouseModel();
            foreach ($param1 as $key => $i) {
                $validator1 = Validator::make($i, $rules1, $message1);
                if ($validator1->fails()) {
                    throw new ApiException($validator1->errors()->first(), 610502);
                }else{
                    $info = $warehouseModel->info(['projectId'=>$request['projectId'],'materialId'=>$i['materialId'],'specId'=>$i['specId']]);
                    if ($i['amount'] > $info['amount']){
                        throw new ApiException($info['materialName'].'退货数量超过库存', 610502);
                    }
                }
            }

            $supplierOrdersReturnedModel = new SupplierOrdersReturnedModel();
            $param['adminId'] = $request->session()->get('adminId');
            $result = $supplierOrdersReturnedModel->returned($param);
            if (!$result){
                throw new ApiException('保存失败', 610503);
            }
        } else {
            throw new ApiException($validator->errors()->first(), 610501);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * 材料调拨发起
     *
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function adjust(Request $request)
    {
        $rules = [
            'projectId' => 'required|integer',
            'newProjectId' => 'required|integer',
            'time' => 'required|date_format:Y-m-d',
            'data' => 'required|array',
        ];
        $message = [
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型不正确',
            'newProjectId.required' => '获取调拨接收方项目参数失败',
            'newProjectId.integer' => '调拨接收方项目参数类型不正确',
            'time.required' => '请选择调拨日期',
            'time.date_format' => '调拨日期格式不正确',
            'data.required' => '请填写材料具体信息',
            'data.array' => '材料具体信息类型不正确',
        ];
        $param = $request->only(['projectId', 'newProjectId', 'data','time','remark']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $rules1 = [
                'materialId' => 'required|integer',
                'specId' => 'required|integer',
                'unitId' => 'required|integer',
                'amount' => 'required|numeric',
            ];
            $message1 = [
                'materialId.required' => '请选择材料',
                'materialId.integer' => '材料参数类型错误',
                'specId.required' => '请选择材料规格',
                'specId.integer' => '材料规格参数类型错误',
                'unitId.required' => '请选择计量单位',
                'unitId.integer' => '计量单位参数类型错误',
                'amount.required' => '请填写材料数量',
                'amount.numeric' => '材料数量类型错误',
            ];
            $param1 = $param['data'];
            $warehouseModel = new WarehouseModel();
            $warehouseAdjustModel = new WarehouseAdjustModel();
            foreach ($param1 as $key => $i) {
                $validator1 = Validator::make($i, $rules1, $message1);
                if ($validator1->fails()) {
                    throw new ApiException($validator1->errors()->first(), 610502);
                }else{
                    $info = $warehouseModel->info(['projectId'=>$request['projectId'],'materialId'=>$i['materialId'],'specId'=>$i['specId']]);
                    if ($i['amount'] > $info['amount']){
                        throw new ApiException($info['materialName'].'调拨数量超过库存', 610502);
                    }
                }
            }

            $param['adminId'] = $request->session()->get('adminId');
            $result = $warehouseAdjustModel->adjust($param);
            if (!$result){
                throw new ApiException('保存失败', 610503);
            }
        } else {
            throw new ApiException($validator->errors()->first(), 610501);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function adjustLists(Request $request)
    {
        $rules = [
            'projectId' => 'required|integer',
            'startTime' => 'nullable|date_format:Y-m-d',
            'endTime' => 'nullable|date_format:Y-m-d',
            'length' => 'nullable|integer|in:10,20,50',
            'page' => 'nullable|integer|min:0',
            'status' => 'nullable|integer|in:0,1,2,3,4',
        ];
        $message = [
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型不正确',
            'startTime.date_format' => '起始时间格式不正确',
            'endTime.date_format' => '截止时间格式不正确',
            'length.integer' => '记录条数参数类型错误',
            'length.in' => '记录条数参数值不正确',
            'page.integer' => '页码参数类型错误',
            'page.min' => '页码参数值不小于:min',
            'status.integer' => '状态参数类型错误',
            'status.in' => '状态取值范围不正确',
        ];
        $param = $request->only(['projectId','startTime','endTime','page','length','status']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $warehouseAdjustModel = new WarehouseAdjustModel();
            $lists = $warehouseAdjustModel->lists($param);
            $countLists = $warehouseAdjustModel->countLists($param);
            $this->data = $this->tableFormat($lists, $countLists);
        } else {
            throw new ApiException($validator->errors()->first(), 610501);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function adjustInfo(Request $request){
        $rules = [
            'id' => 'required|integer',
        ];
        $message = [
            'id.required' => '获取调拨单参数失败',
            'id.integer' => '调拨单参数类型不正确',
        ];
        $param = $request->only(['id']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $warehouseAdjustInfoModel = new WarehouseAdjustInfoModel();
            $this->data = $warehouseAdjustInfoModel->lists($param);
        } else {
            throw new ApiException($validator->errors()->first(), 610501);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function deleteAdjust(Request $request){
        $rules = [
            'id' => 'required|integer',
        ];
        $message = [
            'id.required' => '获取调拨单参数失败',
            'id.integer' => '调拨单参数类型不正确',
        ];
        $param = $request->only(['id']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $warehouseAdjustModel = new WarehouseAdjustModel();
            $this->data = $warehouseAdjustModel->delete($param);
        } else {
            throw new ApiException($validator->errors()->first(), 610501);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function adjustStart(Request $request){
        $rules = [
            'id' => 'required|integer',
            'status' =>'required|integer',
        ];
        $message = [
            'id.required' => '获取调拨单参数失败',
            'id.integer' => '调拨单参数类型不正确',
            'status.required' => '获取调拨单状态失败',
            'status.integer' => '调拨单状态类型不正确',
        ];
        $param = $request->only(['id','status']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $warehouseAdjustModel = new WarehouseAdjustModel();
            $this->data = $warehouseAdjustModel->update(['id'=>$param['id']],['status'=>$param['status']]);
        } else {
            throw new ApiException($validator->errors()->first(), 610501);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function adjustReceive(Request $request){
        $rules = [
            'adjustId' => 'required|integer',
            'projectId' => 'required|integer',
            'receivedTime' => 'required|date_format:Y-m-d',
        ];
        $message = [
            'adjustId.required' => '获取调拨单参数失败',
            'adjustId.integer' => '调拨单参数类型错误',
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型错误',
            'receivedTime.required' => '请选择收货看日期',
            'receivedTime.date_format' => '收货日期格式不正确',
        ];
        $param = $request->only('adjustId','data','projectId','receivedTime');
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $rules1 = [
                'id' => 'required|integer',
                'received' => 'required|numeric',
            ];
            $message1 = [
                'id.required' => '获取参数失败',
                'id.integer' => '参数类型错误',
                'received.required' => '获取收货数量失败',
                'received.numeric' => '收货数量类型错误',
            ];
            $param['adminId'] = $request->session()->get('adminId');
            foreach ($param['data'] as $key => $a) {
                $validator1 = Validator::make($a, $rules1, $message1);
                if (!$validator1->passes()) {
                    throw new ApiException($validator->errors()->first(), 322502);
                }
            }

            $warehouseAdjustModel = new WarehouseAdjustModel();
            $warehouseAdjustModel->receive($param);
        } else {
            throw new ApiException($validator->errors()->first(), 750601);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function adjustComplete(Request $request){
        $rules = [
            'id' => 'required|integer',
            'status' =>'required|integer',
        ];
        $message = [
            'id.required' => '获取调拨单参数失败',
            'id.integer' => '调拨单参数类型不正确',
            'status.required' => '获取调拨单状态失败',
            'status.integer' => '调拨单状态类型不正确',
        ];
        $param = $request->only(['id','status']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $warehouseAdjustModel = new WarehouseAdjustModel();
            $param['adminId'] = $request->session()->get('adminId');
            $this->data = $warehouseAdjustModel->complete($param);
        } else {
            throw new ApiException($validator->errors()->first(), 610501);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function expend(Request $request)
    {
        $rules = [
            'projectId' => 'required|integer',
            'time' => 'required|date_format:Y-m-d',
            'data' => 'required|array',
        ];
        $message = [
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型不正确',
            'time.required' => '请选择消费时间',
            'time.date_format' => '消费时间格式不正确',
            'data.required' => '请填写材料具体信息',
            'data.array' => '材料具体信息类型不正确',
        ];
        $param = $request->only(['projectId', 'time', 'data', 'remark']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            if ($param['time'] > date('Y-m-d')) {
                throw new ApiException('所选时间不能超过当前时间', 610502);
            }
            $rules1 = [
                'id' => 'required|integer',
                'amount' => 'required|numeric|min:0',
            ];
            $message1 = [
                'id.required' => '请选择材料',
                'id.integer' => '材料参数类型错误',
                'amount.required' => '请填写材料数量',
                'amount.numeric' => '材料数量类型错误',
            ];
            $warehouseModel = new WarehouseModel();
            foreach ($param['data'] as $key => $i) {
                $validator1 = Validator::make($i, $rules1, $message1);
                if ($validator1->fails()) {
                    throw new ApiException($validator1->errors()->first(), 610703);
                }
                //检查库存
                $warehouseInfo = $warehouseModel->info([
                    'id' => $i['id'],
                ]);
                $param['data'][$key]['unitId'] = $warehouseInfo['unitId'];
                if (empty($warehouseInfo) || $warehouseInfo['amount'] < $i['amount']) {
                    throw new ApiException($warehouseInfo['materialName'] . '库存不足,剩余' . $warehouseInfo['amount'], 610704);
                }
                if ($warehouseInfo['salePrice']==null) {
                    throw new ApiException($warehouseInfo['materialName'] . '仓库售价未设置' . $warehouseInfo['amount'], 610705);
                }
            }
            $materialModel = new WarehouseModel();
            $param['type'] = WarehouseLogModel::EXPEND;
            $param['adminId'] = $request->session()->get('adminId');
            $result = $materialModel->expend($param);
        } else {
            throw new ApiException($validator->errors()->first(), 610701);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * 材料消费
     *
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function consume(Request $request)
    {
        $rules = [
            'projectId' => 'required|integer',
            'sourceEmployeeId' => 'required|integer',
            'time' => 'required|date_format:Y-m-d',
            'data' => 'required|array',
        ];
        $message = [
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型不正确',
            'sourceEmployeeId.required' => '请选择工人',
            'sourceEmployeeId.integer' => '工人参数类型不正确',
            'time.required' => '请选择消费时间',
            'time.date_format' => '消费时间格式不正确',
            'data.required' => '请填写材料具体信息',
            'data.array' => '材料具体信息类型不正确',
            'isGroup.required' => '获取参数失败',
            'isGroup.integer' => '参数类型不正确',
            'isGroup.in' => '参数值超出取值范围',
        ];
        $param = $request->only(['projectId', 'sourceEmployeeId', 'time', 'data', 'remark']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            if ($param['time'] > date('Y-m-d')) {
                throw new ApiException('所选时间不能超过当前时间', 610802);
            }
            $rules1 = [
                'materialId' => 'required|integer',
                'specId' => 'required|integer',
//                'unitId' => 'required|integer',
                'amount' => 'required|numeric',
            ];
            $message1 = [
                'materialId.required' => '请选择材料',
                'materialId.integer' => '材料参数类型错误',
                'specId.required' => '请选择材料规格',
                'specId.integer' => '材料规格参数类型错误',
//                'unitId.required' => '请选择计量单位',
//                'unitId.integer' => '计量单位参数类型错误',
                'amount.required' => '请填写材料数量',
                'amount.numeric' => '材料数量类型错误',
            ];
            $warehouseModel = new WarehouseModel();
            foreach ($param['data'] as $key => $i) {
                $validator1 = Validator::make($i, $rules1, $message1);
                if ($validator1->fails()) {
                    throw new ApiException($validator1->errors()->first(), 610803);
                }
                //检查库存
                $warehouseInfo = $warehouseModel->info(['projectId'=>$request['projectId'],'materialId'=>$i['materialId'],'specId'=>$i['specId']]);
                $param['data'][$key]['unitId'] = $warehouseInfo['unitId'];
                if (empty($warehouseInfo) || $warehouseInfo['amount'] < $i['amount']) {
                    throw new ApiException($warehouseInfo['materialName'] . '库存不足,剩余' . $warehouseInfo['amount'], 610804);
                }
                if ($warehouseInfo['salePrice']==null) {
                    throw new ApiException($warehouseInfo['materialName'] . '仓库售价未设置' . $warehouseInfo['amount'], 610805);
                }
            }
            $param['type'] = WarehouseLogModel::CONSUME;
            $param['adminId'] = $request->session()->get('adminId');
            $result = $warehouseModel->ex_warehouse($param);
        } else {
            throw new ApiException($validator->errors()->first(), 610801);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * 材料消费
     *
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function groupConsume(Request $request)
    {
        $rules = [
            'projectId' => 'required|integer',
            'sourceGroupId' => 'required|integer',
            'time' => 'required|date_format:Y-m-d',
            'data' => 'required|array',
        ];
        $message = [
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型不正确',
            'sourceGroupId.required' => '请选择班组',
            'sourceGroupId.integer' => '班组参数类型不正确',
            'time.required' => '请选择消费时间',
            'time.date_format' => '消费时间格式不正确',
            'data.required' => '请填写材料具体信息',
            'data.array' => '材料具体信息类型不正确',
        ];
        $param = $request->only(['projectId', 'sourceGroupId', 'time', 'data', 'remark']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            if ($param['time'] > date('Y-m-d')) {
                throw new ApiException('所选时间不能超过当前时间', 611102);
            }
            $rules1 = [
                'materialId' => 'required|integer',
                'specId' => 'required|integer',
//                'unitId' => 'required|integer',
                'amount' => 'required|numeric',
            ];
            $message1 = [
                'materialId.required' => '请选择材料',
                'materialId.integer' => '材料参数类型错误',
                'specId.required' => '请选择材料规格',
                'specId.integer' => '材料规格参数类型错误',
//                'unitId.required' => '请选择计量单位',
//                'unitId.integer' => '计量单位参数类型错误',
                'amount.required' => '请填写材料数量',
                'amount.numeric' => '材料数量类型错误',
            ];
            $warehouseModel = new WarehouseModel();
            foreach ($param['data'] as $key => $i) {
                $validator1 = Validator::make($i, $rules1, $message1);
                if ($validator1->fails()) {
                    throw new ApiException($validator1->errors()->first(), 611103);
                }
                //检查库存
                $warehouseInfo = $warehouseModel->info(['projectId'=>$request['projectId'],'materialId'=>$i['materialId'],'specId'=>$i['specId']]);
                $param['data'][$key]['unitId'] = $warehouseInfo['unitId'];
                if (empty($warehouseInfo) || $warehouseInfo['amount'] < $i['amount']) {
                    throw new ApiException($warehouseInfo['materialName'] . '库存不足,剩余' . $warehouseInfo['amount'], 611104);
                }
                if ($warehouseInfo['salePrice']==null) {
                    throw new ApiException($warehouseInfo['materialName'] . '仓库售价未设置' . $warehouseInfo['amount'], 611105);
                }
            }
            $param['type'] = WarehouseLogModel::GROUP_CONSUME;
            $param['adminId'] = $request->session()->get('adminId');
            $result = $warehouseModel->ex_warehouse($param);
        } else {
            throw new ApiException($validator->errors()->first(), 611101);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function allot(Request $request)
    {
        $rules = [
            'projectId' => 'required|integer',
            'sourceProjectId' => 'required|integer',
            'time' => 'required|date_format:Y-m-d',
            'data' => 'required|array',
        ];
        $message = [
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型不正确',
            'sourceProjectId.required' => '请选择调拨项目',
            'sourceProjectId.integer' => '调拨项目参数类型不正确',
            'time.required' => '请选择消费时间',
            'time.date_format' => '消费时间格式不正确',
            'data.required' => '请填写材料具体信息',
            'data.array' => '材料具体信息类型不正确',
        ];
        $param = $request->only(['projectId', 'sourceProjectId', 'time', 'data', 'remark']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            if ($param['time'] > date('Y-m-d')) {
                throw new ApiException('所选时间不能超过当前时间', 610901);
            }
            $rules1 = [
                'id' => 'required|integer',
                'amount' => 'required|numeric|min:0',
            ];
            $message1 = [
                'id.required' => '请选择材料',
                'id.integer' => '材料参数类型错误',
                'amount.required' => '请填写材料数量',
                'amount.numeric' => '材料数量类型错误',
            ];
            $warehouseModel = new WarehouseModel();
            foreach ($param['data'] as $key => $i) {
                $validator1 = Validator::make($i, $rules1, $message1);
                if ($validator1->fails()) {
                    throw new ApiException($validator1->errors()->first(), 610902);
                }
                //检查库存
                $warehouseInfo = $warehouseModel->info([
                    'id' => $i['id'],
                ]);
                $param['data'][$key]['unitId'] = $warehouseInfo['unitId'];
                if (empty($warehouseInfo) || $warehouseInfo['amount'] < $i['amount']) {
                    throw new ApiException($warehouseInfo['materialName'] . '库存不足,剩余' . $warehouseInfo['amount'], 610803);
                }
            }
            $param['type'] = WarehouseLogModel::ALLOT;
            $param['adminId'] = $request->session()->get('adminId');
            $result = $warehouseModel->ex_warehouse($param);
        } else {
            throw new ApiException($validator->errors()->first(), 610901);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * 报损
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function breakdown(Request $request)
    {
        $rules = [
            'projectId' => 'required|integer',
            'time' => 'required|date_format:Y-m-d',
            'remark' => 'required',
            'data' => 'required|array',
        ];
        $message = [
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型不正确',
            'time.required' => '请选择消费时间',
            'time.date_format' => '消费时间格式不正确',
            'remark.required' => '请输入报损原因',
            'data.required' => '请填写材料具体信息',
            'data.array' => '材料具体信息类型不正确',
        ];
        $param = $request->only(['projectId', 'time', 'remark', 'data']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            if ($param['time'] > date('Y-m-d')) {
                throw new ApiException('所选时间不能超过当前时间', 611002);
            }
            $rules1 = [
                'id' => 'required|integer',
                'amount' => 'required|numeric|min:0',
            ];
            $message1 = [
                'id.required' => '请选择材料',
                'id.integer' => '材料参数类型错误',
                'amount.required' => '请填写材料数量',
                'amount.numeric' => '材料数量类型错误',
            ];
            $warehouseModel = new WarehouseModel();
            foreach ($param['data'] as $key => $i) {
                $validator1 = Validator::make($i, $rules1, $message1);
                if ($validator1->fails()) {
                    throw new ApiException($validator1->errors()->first(), 611002);
                }
                //检查库存
                $warehouseInfo = $warehouseModel->info([
                    'id' => $i['id'],
                ]);
                $param['data'][$key]['unitId'] = $warehouseInfo['unitId'];
                if (empty($warehouseInfo) || $warehouseInfo['amount'] < $i['amount']) {
                    throw new ApiException($warehouseInfo['materialName'] . '库存不足,剩余' . $warehouseInfo['amount'], 610803);
                }
            }
            $param['type'] = WarehouseLogModel::BREAKDOWN;
            $param['adminId'] = $request->session()->get('adminId');
            $result = $warehouseModel->ex_warehouse($param);
        } else {
            throw new ApiException($validator->errors()->first(), 611001);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function materialLists(Request $request){
        $rules = [
            'projectId' => 'required|integer',
        ];
        $message = [
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型不正确',
        ];
        $param = $request->only(['projectId']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $warehouseModel = new WarehouseModel();
            $this->data = $warehouseModel->materialLists($param);
        } else {
            throw new ApiException($validator->errors()->first(), 610101);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws ApiException
     */
    public function specLists(Request $request){
        $rules = [
            'projectId' => 'required|integer',
            'materialId' => 'required|integer',
        ];
        $message = [
            'projectId.required' => '获取项目参数失败',
            'projectId.integer' => '项目参数类型不正确',
            'materialId.required' => '获取项目参数失败',
            'materialId.integer' => '项目参数类型不正确',
        ];
        $param = $request->only(['projectId','materialId']);
        $validator = Validator::make($param, $rules, $message);
        if ($validator->passes()) {
            $warehouseModel = new WarehouseModel();
            $this->data = $warehouseModel->specLists($param);
        } else {
            throw new ApiException($validator->errors()->first(), 610101);
        }
        return $this->ajaxResult($this->code, $this->msg, $this->data);
    }
}